commonlibsse_ng\re\b/
BSIntrusiveRefCounted.rs

1use core::sync::atomic::{AtomicU32, Ordering};
2
3#[repr(C)]
4#[derive(Debug, Default)]
5pub struct BSIntrusiveRefCounted {
6    pub refCount: AtomicU32,
7}
8const _: () = assert!(core::mem::size_of::<BSIntrusiveRefCounted>() == 0x4);
9
10impl BSIntrusiveRefCounted {
11    #[inline]
12    pub const fn new() -> Self {
13        Self { refCount: AtomicU32::new(0) }
14    }
15}
16
17pub trait BSIntrusiveRefCountedTrait {
18    /// Returns the value after +1 to the current value.
19    ///
20    /// # Note
21    /// Implementations must ensure that the returned value is the *new* reference count
22    /// **after incrementing**, not the old one.
23    fn inc_ref(&self) -> u32;
24
25    /// Decrements the reference count and returns the value *after* decrementing.
26    ///
27    /// # Note
28    /// Implementations must ensure that the returned value is the *new* reference count
29    /// **after decrementing**, not the old one.
30    fn dec_ref(&self) -> u32;
31}
32
33impl BSIntrusiveRefCountedTrait for BSIntrusiveRefCounted {
34    #[inline]
35    fn inc_ref(&self) -> u32 {
36        // Reproduction of post-increment of C++ atomic_ref
37        self.refCount.fetch_add(1, Ordering::AcqRel) + 1
38    }
39
40    #[inline]
41    fn dec_ref(&self) -> u32 {
42        // Reproduction of post-decrement of C++ atomic_ref
43        self.refCount.fetch_sub(1, Ordering::AcqRel) - 1
44    }
45}